package xian.woniuxy.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xian.woniuxy.entity.Goods;
import xian.woniuxy.service.IGoodsService;
import xian.woniuxy.service.Impl.GoodsServiceImpl;
import xian.woniuxy.util.ResultVO;

import java.util.List;

/**
 * @author ljy
 * @time 2021/12/25 13:22:47
 */
@RequestMapping("goods")
@RestController
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;

    @PostMapping
    public ResultVO save (@RequestBody Goods goods){

        goodsService.save(goods);

        return new ResultVO(200, "save success", null);

    }

//    @GetMapping
//    public ResultVO getAll(){
//        List<Goods> list = goodsService.getAll();
//        return new ResultVO(200,"getAll success..",list);
//    }

    @DeleteMapping("/{id}")
    public ResultVO delGoodById(@PathVariable int id) {
        goodsService.delete(id);
        return new ResultVO(200, "delGood OK", "null");
    }

    @PutMapping
    public ResultVO updateGood(@RequestBody Goods goods) {
        goodsService.update(goods);
        return new ResultVO(200, "updateGood OK", "null");
    }

    @GetMapping()
    public ResultVO getByName(String name){
        if ("".equals(name)){
            List<Goods> list = goodsService.getAll();
            return new ResultVO(200,"search ok",list);
        }else{
            List<Goods> list = goodsService.getByName(name);
            return new ResultVO(200,"search ok",list);
        }
    }

}
